/**
 * There are code generation
 */
package com.neusoft.lohas.modules.ivm.service.merchant;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.neusoft.lohas.common.persistence.Page;
import com.neusoft.lohas.common.service.BaseService;
import com.neusoft.lohas.common.utils.StringUtils;
import com.neusoft.lohas.modules.ivm.dao.merchant.ActivityPosterDao;
import com.neusoft.lohas.modules.ivm.entity.merchant.ActivityPoster;
import com.neusoft.lohas.modules.ivm.entity.merchant.Merchant;

/**
 * 活动海报Service
 * @author Jackie.Liu
 * @version 2014-10-30
 */
@Component
@Transactional(readOnly = true)
public class ActivityPosterService extends BaseService {

	@Autowired
	private ActivityPosterDao activityPosterDao;
	
	public ActivityPoster get(String id) {
		return activityPosterDao.get(id);
	}
	
	public Page<ActivityPoster> find(Page<ActivityPoster> page, ActivityPoster activityPoster) {
		DetachedCriteria dc = activityPosterDao.createDetachedCriteria();
		if (StringUtils.isNotEmpty(activityPoster.getName())){
			dc.add(Restrictions.like("name", "%"+activityPoster.getName()+"%"));
		}
		dc.add(Restrictions.eq(ActivityPoster.FIELD_DEL_FLAG, ActivityPoster.DEL_FLAG_NORMAL));
		dc.addOrder(Order.desc("id"));
		return activityPosterDao.find(page, dc);
	}
	
	@Transactional(readOnly = false)
	public void save(ActivityPoster activityPoster) {
		activityPosterDao.save(activityPoster);
	}
	
	@Transactional(readOnly = false)
	public void delete(String id) {
		activityPosterDao.deleteById(id);
	}

	public ActivityPoster findByMerchantId(String merchantId) {
		ActivityPoster activityPoster = activityPosterDao.findByMerchantId(merchantId);
		if (activityPoster == null) {
			activityPoster = new ActivityPoster();
			activityPoster.setMerchant(new Merchant(merchantId));
		}
		return activityPoster;
	}
	
}
